package com.example.pro1.controller.lian;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pro1.entity.lian.*;
import com.example.pro1.service.lian.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/good")
@CrossOrigin
public class goodController {
    @Autowired
    com.example.pro1.service.lian.GoodService GoodService;
    @Autowired
    GimgService GimgService;
    @Autowired
    com.example.pro1.service.lian.GoodtogfsService GoodtogfsService;
    @Autowired
    com.example.pro1.service.lian.GoodsecondsortService GoodsecondsortService;
    @Autowired
    com.example.pro1.service.lian.GsstogfsService GsstogfsService;
    @Autowired
    com.example.pro1.service.lian.GoodfirstsortService GoodfirstsortService;

    /*goodController测试接口*/
    @RequestMapping("/test")
    public String goodTest(){
        return "测试成功";
    }

    /*1.根据商品信息查询单个商品信息*/
    @RequestMapping("/getGoodInfoById")
    public Good getGoodInfoById(@RequestParam("gId") String gId){
        return GoodService.getById(gId);
    }

    /*2.根据id列表批量查询商品信息*/
    @RequestMapping("/getGoodsInfoByIds")
    public List<Good> getGoodsInfoByIds(@RequestParam("gIds") List<String> gIds){
        return GoodService.listByIds(gIds);
    }

    /*3.根据商品id查询对应的图片信息*/
    @RequestMapping("/getGimgByGId")
    public List<Gimg> getGimgByGId(@RequestParam("gId") String gId){
        LambdaQueryWrapper<Gimg> LambdaQueryWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper.eq(StringUtils.isNotBlank(gId),Gimg::getGid,gId);
        return GimgService.list(LambdaQueryWrapper);
    }

    /*4.根据商品id查询对象二级分类信息*/
    @RequestMapping("/getGoodsecondsortByGId")
    public Goodsecondsort getGoodsecondsortByGId(@RequestParam("gId") String gId){
        /*首先根据商品id查询对应的二级分类id*/
        LambdaQueryWrapper<Goodtogfs> GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
        GoodtogfsQueryWrapper.eq(StringUtils.isNotBlank(gId),Goodtogfs::getGid,gId);
        Goodtogfs goodtogfs = GoodtogfsService.getOne(GoodtogfsQueryWrapper);
        String gssid = goodtogfs.getGssid();
        /*根据gssId查询gsecondsort信息*/
        LambdaQueryWrapper<Goodsecondsort> GoodsecondsortQueryWrapper = new LambdaQueryWrapper<>();
        GoodsecondsortQueryWrapper.eq(StringUtils.isNotBlank(gssid),Goodsecondsort::getGssid,gssid);
        return GoodsecondsortService.getOne(GoodsecondsortQueryWrapper);
    }

    /*5.根据商品id查询对应一级分类信息*/
    @RequestMapping("/getGoodfirstsortByGId")
    public Goodfirstsort getGoodfirstsortByGId(@RequestParam("gId") String gId){
        Goodsecondsort goodsecondsort = getGoodsecondsortByGId(gId);
        /*根据二级分类信息中的gssId获取对应的gfsId*/
        String gssid = goodsecondsort.getGssid();
        LambdaQueryWrapper<Gsstogfs> GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
        GsstogfsQueryWrapper.eq(StringUtils.isNotBlank(gssid), Gsstogfs::getGssid,gssid);
        Gsstogfs gsstogfs = GsstogfsService.getOne(GsstogfsQueryWrapper);
        String gfsid = gsstogfs.getGfsid();
        /*根据gfsId查询对应的一级分类信息*/
        LambdaQueryWrapper<Goodfirstsort> GoodfirstsortQueryWrapper = new LambdaQueryWrapper<>();
        GoodfirstsortQueryWrapper.eq(StringUtils.isNotBlank(gfsid),Goodfirstsort::getGfsid,gfsid);
        Goodfirstsort Goodfirstsort = GoodfirstsortService.getOne(GoodfirstsortQueryWrapper);
        return Goodfirstsort;
    }


    /*6.返回所有的一级分类信息*/
    @RequestMapping("/getAllGoodFirstSortInfo")
    public List<Goodfirstsort> getAllGoodFirstSortInfo(){
        return GoodfirstsortService.list();
    }


    /*7.根据一级分类id返回对应一级分类信息*/
    @RequestMapping("/getGoodFirstSortInfoById")
    public Goodfirstsort getGoodFirstSortInfoById(@RequestParam("gfsId") String gfsId){
        return GoodfirstsortService.getById(gfsId);
    }

    /*8.根据一级分类id返回对应二级分类信息*/
    @RequestMapping("/getGoodsecondsortListByGoodFirstSortId")
    public List<Goodsecondsort> getGoodsecondsortListByGoodFirstSortId(@RequestParam("gfsId") String gfsId){
        /*首先根据gfsId查询对应的gssId列表*/
        LambdaQueryWrapper<Gsstogfs> GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
        GoodtogfsQueryWrapper.eq(StringUtils.isNotBlank(gfsId),Gsstogfs::getGfsid,gfsId);
        List<Gsstogfs> GsstogfsList = GsstogfsService.list(GoodtogfsQueryWrapper);
        /*遍历GsstogfsList从而得到二级分类id*/
        List<Goodsecondsort> GoodsecondsortList = new ArrayList<>();
        for (int i = 0; i < GsstogfsList.size(); i++) {
            /*得到二级分类的id*/
            String gssid = GsstogfsList.get(i).getGssid();
            /*根据二级分类id获取二级分类对象*/
            Goodsecondsort goodsecondsort = GoodsecondsortService.getById(gssid);
            /*将二级分类对象插入二级分类对象列表中*/
            GoodsecondsortList.add(goodsecondsort);
        }
        return GoodsecondsortList;
    }


    /*9.根据二级分类id返回二级分类信息*/
    @RequestMapping("/getGoodsecondsortById")
    public Goodsecondsort getGoodsecondsortById(@RequestParam("gssId") String gssId){
        return GoodsecondsortService.getById(gssId);
    }


    /*10.根据二级分类id返回对应的商品信息*/
    @RequestMapping("/getGoodListByGoodSecondSortId")
    public List<Good> getGoodListByGoodSecondSortId(@RequestParam("gssId") String gssId){
        /*首先根据gssId查询出对应的gId的list*/
        LambdaQueryWrapper<Goodtogfs> GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
        GoodtogfsQueryWrapper.eq(StringUtils.isNotBlank(gssId),Goodtogfs::getGssid,gssId);
        List<Goodtogfs> GoodtogfsList = GoodtogfsService.list(GoodtogfsQueryWrapper);
        /*遍历GoodtogfsList，获取所有的Good，返回list*/
        /*创建返回的gId*/
        List<Good> goodList = new ArrayList<>();
        for (int i = 0; i < GoodtogfsList.size(); i++) {
            Goodtogfs goodtogfs = GoodtogfsList.get(i);
            /*获取gId*/
            String gid = goodtogfs.getGid();
            /*根据gId获取good*/
            /*返回没有被删除的商品*/
            Good good = getGoodInfoById(gid);
            if (good.getGnumber()!="-1"){
                goodList.add(good);
            }
        }
        return goodList;
    }

    /*11.商品懒加载接口(分页查询)*/
    @RequestMapping("/getLazyGoodsInfoByK")
    public List<Good> getLazyGoodsInfoByK(@RequestParam("pageNum") Integer pageNum,@RequestParam("lineNum") Integer lineNum){
        Page<Good> GoodPage = new Page<>(pageNum,lineNum);
        /*不能返回被删除的商品*/
        LambdaQueryWrapper<Good> GoodQueryWrapper = new LambdaQueryWrapper<>();
        GoodQueryWrapper
                .ne(true,Good::getGnumber,"-1");
        Page<Good> page = GoodService.page(GoodPage,GoodQueryWrapper);
        return page.getRecords();
    }


    /*12.返回当前一共有几个good商品*/
    @RequestMapping("/getGoodsNum")
    public Long getGoodsNum(){
        return GoodService.count();
    }




}
